package co.rcantor.dev.formularios;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class Registro extends Activity{
	
	private TextView mDateDisplay;
    private Button mPickDate;
    private int mYear;
    private int mMonth;
    private int mDay;    

	private static String fecha;
	private static String user1;
	private static String user2;

    static final int DATE_DIALOG_ID = 0;
    
    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }
        };

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registro);
        
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        mPickDate = (Button) findViewById(R.id.pickDate);

        mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        updateDisplay();

        final EditText encuestador = (EditText) findViewById(R.id.encuestador);
        final EditText nombre = (EditText) findViewById(R.id.nombre);
        encuestador.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                  setUser1(encuestador.getText().toString());
                  return true;
                }
                return false;
            }
        });
        nombre.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
            	if (event.getAction() == KeyEvent.ACTION_DOWN) {
            		setUser2(nombre.getText().toString());
                	return true;
                }
                return false;
            }
        });
    }
	
    private void updateDisplay() {
        mDateDisplay.setText(
            new StringBuilder()
            .append(mYear).append("-")
            .append((mMonth + 1)<10?"0"+(mMonth + 1):(mMonth + 1))
            .append("-").append(mDay<10?"0"+mDay:mDay));
        
        setFecha(mDateDisplay.getText().toString());
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);
        }
        return null;
    }
    

    public static String getFecha() {
		return fecha;
	}

	public void setFecha(String fecha) {
		Registro.fecha = fecha;
	}

	public static String getUser1() {
		return user1;
	}

	public void setUser1(String user1) {
		Registro.user1 = user1;
	}

	public static String getUser2() {
		return user2;
	}

	public void setUser2(String user2) {
		Registro.user2 = user2;
	}
	
}
